
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.MyMessage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 获取消息详情请求类
 * 
 * @author ljc
 * @since 2013-12-24 下午4:54:22
 */
public class MessageInfoTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    /**
     * 获取消息详情请求接口
     * 
     * @return
     */
    public MessageInfoHttpResponse request(String userName, int Messageid) {
        try {
            // 填充参数，key-value。key是接口要求传的变量名称
            ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("cmd", ProtocolCmd.CMD_MESSAGE_INFO);
            params.put("username", userName);
            params.put("messageid", Messageid);
            params.put("encrypt", 1);
            // 生成请求包
            HttpBaseRequestPackage requestPackage = new HttpBaseRequestPackage();
            requestPackage.setUserName(userName);
            // 生成响应包
            ResponsePackage responsePackage = new MessageInfoResponsePackage();
            // 生成响应类
            MessageInfoHttpResponse appsHttpResponse = new MessageInfoHttpResponse();

            requestParams.add(params);
            requestPackage.setRequestParams(requestParams);
            mCmdList.add(ProtocolCmd.CMD_MESSAGE_INFO);
            KGHttpClient.request(requestPackage, responsePackage);
            // 响应类赋值
            responsePackage.getResponseData(appsHttpResponse);
            // 返回响应类
            return appsHttpResponse;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 响应包
    class MessageInfoResponsePackage extends HttpBaseResponsePackage<MessageInfoHttpResponse> {

        @Override
        protected void handleResponse(MessageInfoHttpResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "MessageInfoTask= " + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    response.setCode(jsonResp.optString("code"));
                    response.setMessage(jsonResp.optString("message"));
                    response.setPrompt(jsonResp.optString("prompt"));
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    if (ProtocolCmd.CMD_MESSAGE_INFO.equalsIgnoreCase(cmd)) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        if (jsonResult != null) {
                            MyMessage messageInfo = new MyMessage();
                            messageInfo.setTitle(jsonResult.getString("title"));

                            // 内容是是否有加密
                            int isEncrypted = jsonResult.getInt("is_encrypted");
                            if (isEncrypted == 1) {
                                messageInfo.setContent(AppUtil.desDecrypt(jsonResult
                                        .getString("content")));
                            } else {
                                messageInfo.setContent(jsonResult.getString("content"));
                            }
                            messageInfo.setInserttime(jsonResult.getString("inserttime"));
                            messageInfo.setMessageid(jsonResult.getInt("messageid"));
                            response.setMessageInfo(messageInfo);
                        }
                    }
                }
                response.setOk(true);
            } catch (Exception e) {
            }

        }
    }

    /**
     * 响应类，这个类主要用于存储返回数据中的各个字段。
     * 
     * @author ljc
     * @since 2013-12-24 下午5:08:43
     */
    public class MessageInfoHttpResponse extends ResponseMessage {

        /**
         * 
         */
        private static final long serialVersionUID = -1504061987367688010L;

        private MyMessage messageInfo;

        public MyMessage getMessageInfo() {
            return messageInfo;
        }

        public void setMessageInfo(MyMessage messageInfo) {
            this.messageInfo = messageInfo;
        }

    }
}
